﻿#ifndef MODBUSCONTROL_H
#define MODBUSCONTROL_H

#include <QObject>

#include <QModbusTcpClient>
#include <QModbusClient>
#include <QModbusDevice>
#include <QModbusDataUnit>

#include <QVariant>
#include <QDebug>

#include <QTimer>

class ModbusControl : public QObject
{
    Q_OBJECT

    // IP地址
    Q_PROPERTY(QString IpAddr READ IpAddr WRITE setIpAddr NOTIFY IpAddrChanged FINAL)
    // 端口号
    Q_PROPERTY(QString PortNumber READ PortNumber WRITE setPortNumber NOTIFY PortNumberChanged FINAL)



public:
    ModbusControl(QObject *parent = nullptr);
    ~ModbusControl();

    // modbus TCP 对象
    QModbusTcpClient* modbusClient = new QModbusTcpClient();

    // IP地址
    QString test_IP;
    // 端口号
    QString test_Port;

    // 连接参数设定
    void setConnectParameter(QString ip, QString port);

    // 连接到设备
    void connectToPLC();

    // 写入寄存器  16位
    void write_modbus_tcp_HoldingRegisters(QString address, QString newValue);

    // 写双字寄存器  32位
    void write_modbus_tcp_HoldingRegisters_DW(QString address,QString newValue);

    // 浮点数拆分
    void floatResolution(QString setValue, uint16_t *high, uint16_t *low);

    // 将两个16位数据整合
    float combinationToFloat(quint16 valueH,quint16 valueL);

    // 自动操作界面 - 所选配方参数一键写入
    void AutomaticPanel_OneKeyWriteParameter(QString knife,QString roller,
                                             QString stress01,QString stress02,QString stress03,
                                             QString fwdknife01,QString fwdknife02,QString fwdknife03,
                                             QString backknife01,QString backknife02,QString backknife03);

    // 读取当前位置
    void readCurrentLocation();
    // 读取目标位置
    void readAimLocation();
    // 读取待命点位置
    void readStandbyLocation();
    // 读取定位位置
    void readLocation();
    // 读取手动调整速度比率
    void readManualSpeedRate();
    // 读取变频器速度
    void readTransducerSpeed();
    // 读取错误代码
    void readErrCode();
    // 读取IO - 离散输入
    void readIO_DisInput();
    // 读取IO - 线圈
    void readIO_Coils();
    // 读取手动自动速度
    void readAutoAndManualSpeed();
    // 读取后刀偏移量
    void readB_KnifeOffset();
    // 读取原点位置
    void readOriginLocation();
    // 读取限位位置
    void readLimitLocation();
    // 读取补偿
    void readCompensate();
    // 读取报警信号
    void readWarningSignal();
    // 读取版本号
    void readVersoinDescription();

    QString IpAddr() const;
    void setIpAddr(const QString &newIpAddr);

    QString PortNumber() const;
    void setPortNumber(const QString &newPortNumber);



    // 当前位置
    QString knife_CurrLocation;
    QString roller_CurrLocation;
    QString stress01_CurrLocation;
    QString stress02_CurrLocation;
    QString stress03_CurrLocation;
    QString f_knife01_CurrLocation;
    QString f_knife02_CurrLocation;
    QString f_knife03_CurrLocation;
    QString b_knife01_CurrLocation;
    QString b_knife02_CurrLocation;
    QString b_knife03_CurrLocation;

    // 目标位置
    QString knife_AimLocation;
    QString roller_AimLocation;
    QString stress01_AimLocation;
    QString stress02_AimLocation;
    QString stress03_AimLocation;
    QString f_knife01_AimLocation;
    QString f_knife02_AimLocation;
    QString f_knife03_AimLocation;
    QString b_knife01_AimLocation;
    QString b_knife02_AimLocation;
    QString b_knife03_AimLocation;

    // 待命点位置
    QString knife_StandbyLocation;
    QString roller_StandbyLocation;
    QString stress01_StandbyLocation;
    QString stress02_StandbyLocation;
    QString stress03_StandbyLocation;
    QString f_knife01_StandbyLocation;
    QString f_knife02_StandbyLocation;
    QString f_knife03_StandbyLocation;
    QString b_knife01_StandbyLocation;
    QString b_knife02_StandbyLocation;
    QString b_knife03_StandbyLocation;

    // 定位位置
    QString knife_Location;
    QString roller_Location;
    QString stress01_Location;
    QString stress02_Location;
    QString stress03_Location;
    QString f_knife01_Location;
    QString f_knife02_Location;
    QString f_knife03_Location;
    QString b_knife01_Location;
    QString b_knife02_Location;
    QString b_knife03_Location;

    // 变频器速度
    QString transducerSpeed;

    // 手动调整速度比例
    QString manualSpeedRate;

    // IO - 离散输入
    bool knifeA;
    bool knifeB;
    bool rollerA;
    bool rollerB;
    bool scramBtn;

    bool rollerOrigin;
    bool knifeUpLmt;
    bool knifeDownLmt;
    bool transducerWarnning;

    bool knifeUpBtn;
    bool knifeDownBtn;
    bool rollerUpBtn;
    bool rollerDownBtn;

    // IO - 线圈
    bool knifeUpSignal;
    bool knifeDownSignal;
    bool rollerUpSignal;
    bool rollerDownSignal;
    bool manualControlSignal;
    bool runningSignal;

    bool warnningSignal;

    // 错误代码
    QString stress01_ErrCode;
    QString stress02_ErrCode;
    QString stress03_ErrCode;
    QString f_knife01_ErrCode;
    QString f_knife02_ErrCode;
    QString f_knife03_ErrCode;
    QString b_knife01_ErrCode;
    QString b_knife02_ErrCode;
    QString b_knife03_ErrCode;

    // 手动 自动速度
    QString stress_ManualSpeed;
    QString stress_AutoSpeed;
    QString knife_ManualSpeed;
    QString knife_AutoSpeed;

    // 偏移量
    QString b_knife01_Offset;
    QString b_knife02_Offset;
    QString b_knife03_Offset;

    // 原点位置
    QString knife_OriginLocation;
    QString roller_OriginLocation;
    QString stress01_OriginLocation;
    QString stress02_OriginLocation;
    QString stress03_OriginLocation;
    QString f_knife01_OriginLocation;
    QString f_knife02_OriginLocation;
    QString f_knife03_OriginLocation;
    QString b_knife01_OriginLocation;
    QString b_knife02_OriginLocation;
    QString b_knife03_OriginLocation;

    // 限位设置
    QString knife_UpLimit;
    QString knife_DownLimit;
    QString roller_UpLimit;
    QString roller_DownLimit;
    QString stress01_F_Limit;
    QString stress01_B_Limit;
    QString stress02_F_Limit;
    QString stress02_B_Limit;
    QString stress03_F_Limit;
    QString stress03_B_Limit;
    QString f_knife01_UpLimit;
    QString f_knife01_DownLimit;
    QString f_knife02_UpLimit;
    QString f_knife02_DownLimit;
    QString f_knife03_UpLimit;
    QString f_knife03_DownLimit;
    QString b_knife01_UpLimit;
    QString b_knife01_DownLimit;
    QString b_knife02_UpLimit;
    QString b_knife02_DownLimit;
    QString b_knife03_UpLimit;
    QString b_knife03_DownLimit;

    // 补偿设置
    QString knife_UpCpst;
    QString knife_DownCpst;
    QString roller_UpCpst;
    QString roller_DownCpst;
    QString stress01_F_Cpst;
    QString stress01_B_Cpst;
    QString stress02_F_Cpst;
    QString stress02_B_Cpst;
    QString stress03_F_Cpst;
    QString stress03_B_Cpst;
    QString f_knife01_UpCpst;
    QString f_knife01_DownCpst;
    QString f_knife02_UpCpst;
    QString f_knife02_DownCpst;
    QString f_knife03_UpCpst;
    QString f_knife03_DownCpst;
    QString b_knife01_UpCpst;
    QString b_knife01_DownCpst;
    QString b_knife02_UpCpst;
    QString b_knife02_DownCpst;
    QString b_knife03_UpCpst;
    QString b_knife03_DownCpst;

    // 报警信号
    bool knife_Up_SLmt;
    bool knife_Down_SLmt;
    bool roller_Up_SLmt;
    bool roller_Down_SLmt;
    bool stress01_F_SLmt;
    bool stress01_B_SLmt;
    bool stress02_F_SLmt;
    bool stress02_B_SLmt;
    bool stress03_F_SLmt;
    bool stress03_B_SLmt;
    bool f_knife01_Up_SLmt;
    bool f_knife01_Down_SLmt;
    bool f_knife02_Up_SLmt;
    bool f_knife02_Down_SLmt;
    bool f_knife03_Up_SLmt;
    bool f_knife03_Down_SLmt;
    bool b_knife01_Up_SLmt;
    bool b_knife01_Down_SLmt;
    bool b_knife02_Up_SLmt;
    bool b_knife02_Down_SLmt;
    bool b_knife03_Up_SLmt;
    bool b_knife03_Down_SLmt;

    QString vertion_Description;

signals:

    void IpAddrChanged();

    void PortNumberChanged();


private:

    QString m_IpAddr;
    QString m_PortNumber;


};

#endif // MODBUSCONTROL_H
